//
//  MyPushViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/29.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "MyPushViewController.h"
#import "MePushTableViewCell.h"
#import "PushMessageInfoViewController.h"
#import "Common.h"
#import "BGMUser.h"
#import "BGMHTTP.h"
#import "MJRefresh.h"


@interface MyPushViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation MyPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+70;
    self.titleBtn.frame = rect;
    self.currentPage = 1;
    self.dataArray = [[NSMutableArray alloc]init];
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height, SCREENW, SCREENH-self.navview.frame.size.height)];
    self.myTableView.backgroundColor = [UIColor clearColor];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        self.currentPage = 1;
        [self getMePush];
    }];
    
    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.currentPage==0) {
            [self.myTableView.mj_footer endRefreshing];
        }else{
            [self getMePush];
        }
    }];
    
    [self.myTableView.mj_header beginRefreshing];
    if (self.isOther) {
        self.userId = self.userId;
        [self.titleBtn setTitle:@"他的发布" forState:UIControlStateNormal];
    }else{
        self.userId = [BGMUser shareInstance].userId;
        [self.titleBtn setTitle:@"我的发布" forState:UIControlStateNormal];
    }
    
}
- (void)getMePush{
     BGMUser *user = [BGMUser shareInstance];
    NSDictionary *dic = @{@"userToken":user.userToken,
                          @"userId":self.userId,
                          @"pageNum":[NSString stringWithFormat:@"%d",self.currentPage],
                          @"pageSize":@"10"
                        };
    [BGMHTTP requestWithMethodName:@"users/getUsersPublishList" parameters:dic completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSArray *array = [result objectForKey:@"data"];
        [self freshTableView:array];
    }];
}
- (void)freshTableView:(NSArray *)array{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        [self.dataArray addObjectsFromArray:array];
        [self.myTableView reloadData];
        if (array.count == 10) {
            self.currentPage = self.currentPage +1;
        }else{
            self.currentPage = 0;
        }
        
        if (self.dataArray.count == 0) {
            self.mormalLabel.hidden = NO;
            self.myTableView.hidden = YES;
        }else{
            self.mormalLabel.hidden = YES;
            self.myTableView.hidden = NO;
        }
        
    });
}
#pragma mark UITABLEVIEWDELEAGE
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;//(SCREENH-70)/5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"CollectionTableViewCell";
    MePushTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MePushTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    cell.messageLabel.text = [dic objectForKey:@"pTitle"];
 
    [cell.forwardBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"forwarfCount"]] forState:UIControlStateNormal];
    
    [cell.commentBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"commentSize"]] forState:UIControlStateNormal];
    
    cell.timeLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pcreatTime"]];
    NSString *imageName = [NSString stringWithFormat:@"push_%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"tpId"]]];
    [cell.headImage setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
 
    NSString *str1 = [NSString stringWithFormat:@"%@",[dic objectForKey:@"redPackge"]];
    float red = [str1 floatValue];
    if (red>0) {
        cell.label1.hidden = NO;
    }else{
        cell.label1.hidden = YES;
    }
    int number = [[NSString stringWithFormat:@"%@",[dic objectForKey:@"pIsItPaid"]]intValue];
    if (number == 1) {
        cell.label2.text = @"有偿";
    }else{
        cell.label2.text = @"无偿";
    }
    number = [[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishAcceptancesSize"]]intValue];
       [cell.acceptBtn setTitle:[NSString stringWithFormat:@"%d",number] forState:UIControlStateNormal];
    
    
    
    
    return cell;
    
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    PushMessageInfoViewController *vc = [[PushMessageInfoViewController alloc]init];
    vc.messageId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pId"]];
    vc.isMe = self.isOther ? NO : YES;
    
    NSString *helpUsersId;
    if ([[dic objectForKey:@"helpUsersId"] isKindOfClass:[NSString class]]) {
        helpUsersId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"helpUsersId"]];
    }else{
        helpUsersId = @"";
    }
    NSString *connectionUserId;
    if ([[dic objectForKey:@"connectionUserId"] isKindOfClass:[NSString class]]) {
        connectionUserId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"connectionUserId"]];
    }else{
        connectionUserId = @"";
    }
    
      NSString *pushType = [NSString stringWithFormat:@"%@",[dic objectForKey:@"tpId"]];
    if ([pushType intValue]>8) {
        vc.isPublicMsg = YES;
    }else{
        vc.isPublicMsg = NO;
    }

    vc.helpUsersId = helpUsersId;
    vc.connectionUserId = connectionUserId;
    [self.navigationController pushViewController:vc animated:YES];

    
}
 



@end
